//
//  ZKJPopAnimation.swift
//  SinaWeiBo
//
//  Created by ZKJ on 2017/8/30.
//  Copyright © 2017年 ZKJ. All rights reserved.
//

import UIKit

class ZKJPopAnimation: NSObject {
    
    // 对外提供的属性
    var isPresent : Bool = false
    var popFrame : CGRect = CGRect.zero
    var callBack : (_ isPresent : Bool) -> ()
    
    // MARK:- 自定义构造函数
    // 注意:如果自定义了一个构造函数,但是没有对默认构造函数init()进行重写,那么自定义的构造函数会覆盖默认的init()构造函数
    // 注意:在闭包中如果使用当前对象的属性或者调用方法,也需要加self
    // 两个地方需要使用self : 1> 如果在一个函数中出现歧义 2> 在闭包中使用当前对象的属性和方法也需要加self
    init(callBack : @escaping (_ isPresent : Bool) -> ()) {
        self.callBack = callBack
    }
}

// MARK:- 自定义转场代理的方法
extension ZKJPopAnimation : UIViewControllerTransitioningDelegate {
    
    // MARK:- 目的:改变弹出View的尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let zkjPresentVC = ZKJPresentationVC(presentedViewController: presented, presenting: presenting)
        zkjPresentVC.presentedFrame = popFrame
        return zkjPresentVC
    }
    
    // MARK:- 目的:自定义弹出的动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        callBack(isPresent)
        return self
    }
    
    // 目的:自定义消失的动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        callBack(isPresent)
        return self
    }
}

// MARK:- 弹出和消失动画代理的方法
extension ZKJPopAnimation : UIViewControllerAnimatedTransitioning {
    
    // MARK:- 动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.25
    }
    
    /// 获取`转场的上下文`:可以通过转场上下文获取弹出的View和消失的View
    // UITransitionContextFromViewKey : 获取消失的View
    // UITransitionContextToViewKey : 获取弹出的View
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresent ? animationPresentedView(transitionContext: transitionContext) : animationDismissView(transitionContext: transitionContext)
    }
    
    // MARK:- 自定义弹出动画
    fileprivate func animationPresentedView(transitionContext: UIViewControllerContextTransitioning) {
        // 1.获取弹出的View
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        
        // 2.将弹出的View添加到containerView中
        transitionContext.containerView.addSubview(presentedView)
        
        // 3.执行动画
        presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        // 改变锚点(默认0.5，0.5)
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.transform = CGAffineTransform.identity
        }) { (true) in
            // 必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
    }
    
    // MARK:- 自定义消失动画
    fileprivate func animationDismissView(transitionContext: UIViewControllerContextTransitioning) {
        // 1.获取消失的View
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        
        // 2.执行动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            dismissView.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)
        }) { (true) in
            dismissView.removeFromSuperview()
            // 必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
    }
}
